package sdstcom.lbq.one.lbq.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.Model.music.Music;
import com.example.antivity.MovieActivity;
import com.example.antivity.MusicActivity;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;

import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.DbBean;
import sdstcom.lbq.one.lbq.LBQapplication;
import sdstcom.lbq.one.lbq.adapter.CollectAdapter;

public class LBQmusicActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private ListView listView;
    private ImageView img;


    private CollectAdapter adapter;
    private DbManager manager;
    private ArrayList<DbBean> list;
    private ArrayList<DbBean> adapterlist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_lbqmusic);
        init();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img.setImageResource(R.mipmap.no_stow_pic);
        listView.setEmptyView(img);
        adapterlist.clear();
        try {
            list= (ArrayList<DbBean>) manager.findAll(DbBean.class);

              for (int i = 0; i <list.size() ; i++) {
                if (list.get(i).getType().equals("music"))
                {
                    adapterlist.add(list.get(i));
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        adapter=new CollectAdapter(adapterlist,this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(LBQmusicActivity.this, MusicActivity.class).putExtra("itemid",adapterlist.get(position).getItemid()));}

        });
    }
    private void init() {
        toolbar= (Toolbar) findViewById(R.id.collectmusic_toolbar);
        listView= (ListView) findViewById(R.id.collectmusic_listview);
        img= (ImageView) findViewById(R.id.collectmusic_empty_img);
        manager= x.getDb(((LBQapplication)getApplication()).config);
        list=new ArrayList<>();
        adapterlist=new ArrayList<>();
    }
    public void click(View view) throws DbException {
        list= (ArrayList<DbBean>) manager.findAll(DbBean.class);
        adapterlist.clear();
        if (list.size()!=0)
         for (int i = 0; i <list.size() ; i++) {
            if (list.get(i).getType().equals("music"))
            {
                adapterlist.add(list.get(i));
            }
        }
        adapter.notifyDataSetChanged();

    }
}
